package com.maoshi.shop.config;


import com.maoshi.shop.common.bean.*;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.service.SysConfigService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商城配置文件
 * 支付配置、文件上传配置、短信配置、快递配置、小程序配置、微信网页开发配置、公众号配置
 */
@Getter
@Component
public class ShopPconfig {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 七牛云的配置信息
     */
    private Qiniu qiniu;
    /**
     * oss 文件上传配置信息
     */
    private Oss oss;

    /**
     * 阿里大于短信平台
     */
    private ALiDaYu aLiDaYu;

    /**
     * 快递鸟物流
     */
    private QuickBird quickBird;
    /**
     * 微信小程序
     */
    private WxMiniApp wxMiniApp;
    /**
     * 微信公众号
     */
    private WxMp wxMp;
    /**
     * 微信支付
     */
    private WxPay wxPay;
    /**
     * 阿里支付
     */
    private Alipay alipay;

    public Qiniu getQiniu(){
        // 从数据库 / 缓存中获取到配置文件信息
        return sysConfigService.getSysConfigObject(Constant.QINIU_CONFIG, Qiniu.class);
    }

    public Oss getOss() {
        return sysConfigService.getSysConfigObject(Constant.OSS_CONFIG, Oss.class);
    }

    public ALiDaYu getALiDaYu(){
        return sysConfigService.getSysConfigObject(Constant.ALIDAYU_CONFIG,ALiDaYu.class);
    }


    public QuickBird getQuickBird() {
        QuickBird quickBird = sysConfigService.getSysConfigObject(Constant.QUICKBIRD_CONFIG, QuickBird.class);
        if (quickBird == null){
            return null;
        }
        String value = sysConfigService.getValue(Constant.QUICKBIRD_CONFIG);
        // Json无法解析 eBusinessID
        quickBird.setEBusinessID(getEBusinessIDByStr(value));
        return quickBird;
    }


    public WxMiniApp getWxMiniApp() {
        return sysConfigService.getSysConfigObject(Constant.MA_CONFIG,WxMiniApp.class);
    }

    public WxMp getWxMp() {
        return sysConfigService.getSysConfigObject(Constant.MP_CONFIG,WxMp.class);
    }

    public WxPay getWxPay() {
        return sysConfigService.getSysConfigObject(Constant.WXPAY_CONFIG,WxPay.class);
    }

    public Alipay getAlipay() {
        return sysConfigService.getSysConfigObject(Constant.ALIPAY_CONFIG,Alipay.class);
    }


    private String getEBusinessIDByStr(String paramValue){
        String[] split = paramValue.split(",");
        for (String s : split) {
            if (s.contains("eBusinessID")){
                String[] split1 = s.split(":");
                String str = split1[1];
                String replace = str.replace("\"", "");
                return replace;
            }
        }
        return "";
    }

}
